package com.asset.salesforce.dao;

import java.util.ArrayList;
import java.util.List;


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.asset.salesforce.domain.Account;
import com.asset.salesforce.domain.Contact;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.FollowCenter;
import com.asset.salesforce.domain.Lead;
import com.asset.salesforce.enums.FOLLOWTYPE;


@Repository
public class FollowCenterDaoImpl {
	@Autowired
	SessionFactory sessionFactory;
	
	public void addfollowCenter(FollowCenter followCenter) {
		try{
			sessionFactory.getCurrentSession().save(followCenter);
			}catch(Exception ex){
				ex.printStackTrace();
			}
	}
	@SuppressWarnings("unchecked")
	public List<FollowCenter> checkIfCurrentFollowThatLead(Employee employee,Lead lead){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from FollowCenter where follower.id = :empId and followedType= :followedId and followedId = :leadId");
		query.setParameter("empId", employee.getId());
		query.setParameter("followedId", FOLLOWTYPE.Lead.getType());
		query.setParameter("leadId", lead.getId());
		List<FollowCenter> centers  = new ArrayList<FollowCenter>();
		centers =  query.list();
		return centers;		
	}
	
	@SuppressWarnings("unchecked")
	public List<FollowCenter> checkIfCurrentFollowThatAccount(Employee employee,Account account){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from FollowCenter where follower.id = :empId and followedType= :followedId and followedId = :accountId");
		query.setParameter("empId", employee.getId());
		query.setParameter("followedId", FOLLOWTYPE.Account.getType());
		query.setParameter("accountId", account.getId());
		List<FollowCenter> centers  = new ArrayList<FollowCenter>();
		centers =  query.list();
		return centers;		
	}
	@SuppressWarnings("unchecked")
	public List<FollowCenter> checkIfCurrentFollowThatContact(Employee employee,Contact contact){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from FollowCenter where follower.id = :empId and followedType= :followedId and followedId = :accountId");
		query.setParameter("empId", employee.getId());
		query.setParameter("followedId", FOLLOWTYPE.Contact.getType());
		query.setParameter("accountId", contact.getId());
		List<FollowCenter> centers  = new ArrayList<FollowCenter>();
		centers =  query.list();
		return centers;		
	}
	@SuppressWarnings("unchecked")
	public List<FollowCenter> checkIfCurrentFollowThatPeople(Employee follower,Employee followed){
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from FollowCenter where follower.id = :empId and followedType= :followedId and followedId = :accountId");
		query.setParameter("empId", follower.getId());
		query.setParameter("followedId", FOLLOWTYPE.People.getType());
		query.setParameter("accountId", followed.getId());
		List<FollowCenter> centers  = new ArrayList<FollowCenter>();
		centers =  query.list();
		return centers;		
	}

}
